﻿<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "ychcomwx");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();

class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
				$RX_TYPE = trim($postObj->MsgType);
				switch ($RX_TYPE)
				{
					case "text":
						$resultStr = $this->receiveText($postObj);
						break;
					case "event":
						$resultStr = $this->receiveEvent($postObj);
						break;
					default:
						$resultStr = "unknow msg type: ".$RX_TYPE;
						break;
				}			
                echo $resultStr;

        }else {
        	echo "";
        	exit;
        }
    }
		
	 private function receiveText($object)
    {
        $funcFlag = 0;
        $keyword = trim($object->Content);
        $resultStr = "";
        $contentStr = "";
     
        $contentStr = '我们已经收到您的信息了，谢谢！';
        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
        return $resultStr;
    }
	
	private function receiveEvent($object)
    {
        $contentStr = "";
         switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "欢迎来到云创测试平台 平台网址：http://demo.yunchsoft.com";
				$resultStr = $this->transmitText($object, $contentStr);
				break;
            case "unsubscribe":
                $contentStr = "";
				$resultStr = $this->transmitText($object, $contentStr);
                break;
            case "CLICK":
				 $dateArray = array();
                switch ($object->EventKey)
                {
                    case "about":
						$contentStr = "公司名称：深圳市网格互联科技有限公司 \n地址：深圳市福田区彩田南路中深花园A座3011 \n电话：400-042-6100 0755 83269293 \n传真：+86 0755 83269293 \n网址：http://demo.yunchsoft.com  \n官方QQ群: 2567806022";
						$resultStr = $this->transmitText($object, $contentStr);		
                        break;
					case "introduction":	
						$dateArray[] = array("Title"=>"云创简介--深圳市网格互联科技有限公司", 
                                "Description"=>"云创P2P网贷系统是基于“一切为客户的需求为向导”这一理念，云创软件产品六大功能模块潜心为中国P2P网贷行业发展而开发的一套网贷系统。", 
                                "Picurl"=>"http://demo.yunchsoft.com/static/images/logo.png", 
                                "Url" =>"http://demo.yunchsoft.com/ychweixin/introduction_view");
						$resultStr = $this->transmitNews($object, $dateArray);
						break;
					case "activity":
						$url = "http://demo.yunchsoft.com/ychweixin/articity";
						$content = file_get_contents($url);
						$info = json_decode($content);
						foreach($info as $v){
							$dateArray[] = array("Title"=>$v->title, "Description"=>"", "Picurl"=>"http://demo.yunchsoft.com/static/data/upfiles/article/".$v->pic, "Url" =>"http://demo.yunchsoft.com/articityview/".$v->id);
						}						
						$resultStr = $this->transmitNews($object, $dateArray);			
						break;
					case "development":
						$dateArray[] = array("Title"=>"云创软件发展历程", 
                                "Description"=>"目前云创P2P网贷系统从基础版到定制版累计已服务多家客户，先后为云创、钱贷贷、五洲财富、中天易贷、每天每贷、天之源贷、净净贷、源达贷、华夏商贷等P2P平台提供了P2P网贷系统解决方案，并获得了客户高度的赞扬和市场的认可。", 
                                "Picurl"=>"http://demo.yunchsoft.com/static/images/dem.jpg", 
                                "Url" =>"http://demo.yunchsoft.com/ychweixin/development_view");
						$resultStr = $this->transmitNews($object, $dateArray);	
						break;	
					case "baozheng":
						$dateArray[] = array("Title"=>"云创简介本金保障", 
                                "Description"=>"华南地区网贷服务基地——倾力打造“网贷”第一品牌", 
                                "Picurl"=>"http://demo.yunchsoft.com/static/images/temp.jpg", 
                                "Url" =>"http://demo.yunchsoft.com/ychweixin/baozheng_view");
						$resultStr = $this->transmitNews($object, $dateArray);	
						break;	
                    default:
						$contentStr = "云创软件";
						$resultStr = $this->transmitText($object, $contentStr);	
						break;
                }
				
                break;
            default:
                $contentStr = "receive a new event: ".$object->Event;
				$resultStr = $this->transmitText($object, $contentStr);
                break;      

       }
        
        return $resultStr;
    }
	
	 private function transmitNews($object, $arr_item, $flag = 0)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['Picurl'], $item['Url']);

        $newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";

        $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $flag);
        return $resultStr;
    }
	
	private function transmitText($object, $content, $flag = 0)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }
     

	
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>